<?php

//Call this file only from server
if ($_SERVER["REMOTE_ADDR"] != $_SERVER["SERVER_ADDR"]) die("Invalid Request"); 
    
	include("../config.php");
	include("../inc/common.php");
    include("../inc/class.database.php");
	include("../inc/db_connect.php");
	
	$result = $db->query( "SELECT COUNT(*) FROM games 
	WHERE LOWER(map) LIKE LOWER('%dota%') AND stats = 0 AND duration>='".$MinDuration."'" );
    $r = $db->fetch_row($result);
    $Total = $r[0];
	
	if ( $Total>=1 ) {
	
	$_result = $db->query( "SELECT id FROM games 
	WHERE LOWER(map) LIKE LOWER('%dota%') AND stats = 0 AND duration>='".$MinDuration."' LIMIT ".$updateGamesCron." " );
	
	while ($row = $db->fetch_array($_result,'assoc')) {
	 $gid = $row["id"];
	 $result = $db->query("SELECT winner, dp.gameid, gp.colour, newcolour, kills, deaths, assists, creepkills, creepdenies, neutralkills, towerkills, gold,  raxkills, courierkills, g.duration as duration,
	   gp.name as name, 
	   gp.ip as ip,
	   b.name as banname 
	   FROM dotaplayers AS dp 
	   LEFT JOIN gameplayers AS gp ON gp.gameid = dp.gameid and dp.colour = gp.colour 
	   LEFT JOIN dotagames AS dg ON dg.gameid = dp.gameid 
	   LEFT JOIN games AS g ON g.id = dp.gameid 
	   LEFT JOIN bans as b ON b.name=gp.name
	   WHERE dp.gameid='".$gid."'
	   GROUP by gp.name
	   ORDER BY newcolour");
	   
	   if ($db->num_rows($result) <=0)  $update = $db->query("UPDATE games SET stats = 1 WHERE id = $gid ;");
	   
	   while ($list = $db->fetch_array($result,'assoc')) {
		$kills=$list["kills"];
		$deaths=$list["deaths"];
		$assists=$list["assists"];
		$creepkills=$list["creepkills"];
		$creepdenies=$list["creepdenies"];
		$neutralkills=$list["neutralkills"];
		$towerkills=$list["towerkills"];
		$raxkills=$list["raxkills"];
		$courierkills=$list["courierkills"];
		$duration=$list["duration"];
		$name=mysql_real_escape_string(trim($list["name"]));
		$IPaddress = $list["ip"];
		$banname=$list["banname"];
		$win=$list["winner"];
		$newcolour=$list["newcolour"];
		
		if ( strtolower($banname)==strtolower($name) ) $BANNED = 1; else $BANNED = 0;
		
		if ($win==1 AND $newcolour<=5) {$winner = 1; $loser = 0;}
		if ($win==0) {$winner = 0; $loser = 0;}
		if ($win==2 AND $newcolour>5) {$winner = 1; $loser = 0;}
		if ($win==1 AND $newcolour>5) {$winner = 0; $loser = 1;}
		if ($win==2 AND $newcolour<=5) {$winner = 0; $loser = 1;}
		
		if ($winner == 1) $score = $ScoreStart + $ScoreWins;
		if ($winner == 0) $score = $ScoreStart + $ScoreLosses;
		if ($win==0) $score = $ScoreStart;
		
		// Score formula for each game (uncomment below to user score formula)
		// $scoreFormula = (((($kills-$deaths+$assists*0.5+$towerkills*0.5+$raxkills*0.2+($courierkills+$creepdenies)*0.1+$neutralkills*0.03+$creepkills*0.03) * .2)+($score)));
		
		// $score = $scoreFormula;
		
		if ($win==0) $draw = 1; else $draw = 0;
		if (!empty($name) AND $duration >= $MinDuration) {
		$result2 = $db->query("SELECT player FROM stats WHERE LOWER(player) = LOWER('$name')");
		//Create a new player...
		  if ( $db->num_rows($result2) <=0) {
          $sql3 = "INSERT INTO stats(player, score, games, wins, losses, draw, kills, deaths, assists, creeps, denies, neutrals, towers, rax, banned, ip) 
		  VALUES('$name',$score,1,$winner,$loser,$draw,$kills,$deaths,$assists,$creepkills,$creepdenies,$neutralkills, $towerkills, $raxkills, $BANNED, '$IPaddress')";
          } else {
		  //...or update player data
		  if ($winner == 1) $score = "score = score + $ScoreWins,";
		  if ($winner == 0) $score = "score = score - $ScoreLosses,";
		  if ($win==0) $score = "";
		  $sql3 = "UPDATE stats SET 
		  $score
		  player = '$name',
		  games = games+1, 
		  wins = wins +$winner,
		  losses = losses+$loser,
		  draw = draw + $draw,
		  kills = kills + $kills,
		  deaths = deaths + $deaths,
		  assists = assists + $assists,
		  creeps = creeps + $creepkills,
		  denies = denies + $creepdenies,
		  neutrals = neutrals + $neutralkills,
		  towers = towers + $towerkills,
		  rax = rax + $raxkills,
          banned = $BANNED,
		  ip = '$IPaddress'
		  WHERE LOWER(player) = LOWER('$name');";
		   }
		  $result3 = $db->query($sql3);
		 }
		 //$return.="\nGame ($gid) updated!";
	     //Update "games" table so we can know what games have been updated
	     $update = $db->query("UPDATE games SET stats = 1 WHERE id = $gid ;");
		 
	   }
	   $return.="\nGame ($gid) updated!";
	}
	
  }
?>